//
//  ShoppingCarHeaderView.swift
//  sss
//
//  Created by Mac on 2021/1/13.
//  Copyright © 2021 hrrMac. All rights reserved.
//

import UIKit

class ShoppingCarHeaderView: UITableViewHeaderFooterView {

    /// 选择按钮
    let selectBtn = UIButton()
    
    /// 选择按钮的文字提示
    let shopNameLab = UILabel()
    /// 商店图标
    let shopIconImgView = UIImageView()
    /// 商店可点击按钮
    let shopRightImgView = UIImageView()
    /// 点击商店按钮
    let shopBtn = UIButton()
    /// 更多按钮
    let moreBtn = UIButton()
    /// 底部分隔线
    let bottomLine = UIView()
    
    /// 全选按钮事件 【是否全选】
    var touchSelectEvent: ((Bool)->Void)?
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    private func configUI(){
        // 选择按钮正常状态的图片
        selectBtn.setImage(UIImage(named: "sc_select_no"), for: .normal)
        // 选择按钮选中状态下的图片
        selectBtn.setImage(UIImage(named: "sc_select_yes"), for: .selected)
        selectBtn.imageView?.contentMode = .scaleAspectFit
        selectBtn.addTarget(self, action: #selector(clickSelectBtn), for: .touchUpInside)
        
        shopIconImgView.image = UIImage(named: "shop_icon")
        shopRightImgView.image = UIImage(named: "shop_right")
        
        shopNameLab.textColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        shopNameLab.font = UIFont.systemFont(ofSize: 14)
        shopNameLab.text = ""
        
        bottomLine.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        
        addSubview(selectBtn)
        addSubview(shopIconImgView)
        addSubview(shopNameLab)
        addSubview(shopRightImgView)
        addSubview(shopBtn)
        addSubview(moreBtn)
        addSubview(bottomLine)
        
        refreshFrame()
        
    }
    
    // 刷新视图的布局
    private func refreshFrame() {
        
        selectBtn.translatesAutoresizingMaskIntoConstraints = false
        selectBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        selectBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        selectBtn.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        selectBtn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        
        shopIconImgView.translatesAutoresizingMaskIntoConstraints = false
        shopIconImgView.leftAnchor.constraint(equalTo: selectBtn.rightAnchor).isActive = true
        shopIconImgView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        shopIconImgView.widthAnchor.constraint(equalToConstant: 24).isActive = true
        shopIconImgView.heightAnchor.constraint(equalToConstant: 24).isActive = true
        
        shopNameLab.translatesAutoresizingMaskIntoConstraints = false
        shopNameLab.leftAnchor.constraint(equalTo: shopIconImgView.rightAnchor, constant: 12).isActive = true
        shopNameLab.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        
        shopRightImgView.translatesAutoresizingMaskIntoConstraints = false
        shopRightImgView.leftAnchor.constraint(equalTo: shopNameLab.rightAnchor,constant: 12).isActive = true
        shopRightImgView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        shopRightImgView.widthAnchor.constraint(equalToConstant: 16).isActive = true
        shopRightImgView.heightAnchor.constraint(equalToConstant: 16).isActive = true
        
        shopBtn.translatesAutoresizingMaskIntoConstraints = false
        shopBtn.leftAnchor.constraint(equalTo: shopIconImgView.leftAnchor).isActive = true
        shopBtn.rightAnchor.constraint(equalTo: shopRightImgView.rightAnchor).isActive = true
        shopBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        shopBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        
        moreBtn.rightAnchor.constraint(equalTo: self.rightAnchor,constant: -15).isActive = true
        moreBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        moreBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        
        bottomLine.translatesAutoresizingMaskIntoConstraints = false
        bottomLine.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        bottomLine.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 15).isActive = true
        bottomLine.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 15).isActive = true
        bottomLine.heightAnchor.constraint(equalToConstant: 0.5).isActive = true

    }
    
    
    override func layoutSubviews() {
        refreshFrame()
    }
}


extension ShoppingCarHeaderView{
    /// 点击全选的
    @objc func clickSelectBtn() {
        selectBtn.isSelected = !selectBtn.isSelected
        if touchSelectEvent != nil {
            touchSelectEvent!(selectBtn.isSelected)
        }
    }
}
